package com.example.controller;

import com.example.mapper.UsersMapper;
import com.example.model.Users;
import com.example.model.UsersExample;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//请阅读readme.txt文件
import java.util.List;

/*
 * @program:demo
 * @description:测试
 * @author:zhangzhen
 * @create: 2018-11-08 17:10
 **/
@Controller
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UsersMapper usersMapper;

    @RequestMapping("/login")
    public String login (Users user, Model model){
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria  criteria=usersExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andPasswordEqualTo(user.getPassword());
        //打印日志
        log.info("Start to verify User [{}]", user);
        List<Users> users = usersMapper.selectByExample(usersExample);
        log.info("查询到的数据为 [{}]", users);
        if(users.size()!=0){
            return "main";
        }
       return "error";
    }

    @RequestMapping("/index/{pageNum}/{pageSize}")
    public String view(Model model, @PathVariable("pageNum")int pageNum, @PathVariable("pageSize") int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        UsersExample usersExample = new UsersExample();
        usersExample.setDistinct(true);
        usersExample.setOrderByClause("id");
        List<Users> users = usersMapper.selectByExample(usersExample);
        model.addAttribute("list",users);
        return "main";
    }
    @RequestMapping("/update")
    public String update(Users user,Model model){
        usersMapper.updateByPrimaryKeySelective(user);
        return "redirect:/index/1/3";
    }
    @RequestMapping("/selectOne")
    public String selectOne(Users users,Model model){
        Users users1 = usersMapper.selectByPrimaryKey(users.getId());
        model.addAttribute("user",users1);
        return "selectOne";
    }
}
